﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Swoosh.Utilities.TraceLogger;
using Swoosh.Contracts.MediaPlayerLib;

namespace Swoosh
{
    public partial class MainTray : Form
    {

        private KeyHook _KeyHook;
        public KeyHook KeyHook
        {
            get { return _KeyHook; }
            set { _KeyHook = value; }
        }

        public MainTray()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        private void mi_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainTray_Load(object sender, EventArgs e)
        {
            this.Visible = false;
            this.ShowInTaskbar = false;

            _KeyHook = new KeyHook();
            _KeyHook.CombinedKeyUp += new CombinedKeyEventHandler(_KeyHook_CombinedKeyUp);
            _KeyHook.CombinedKeyDown += new CombinedKeyEventHandler(_KeyHook_CombinedKeyDown);
        }

        void _KeyHook_CombinedKeyDown(List<Keys> keys)
        {
            Logger.Log(LogLevels.Trace, "DOWN: " + string.Join(", ", keys.Select(kp => kp.ToString()).ToList()));
        }

        void _KeyHook_CombinedKeyUp(List<Keys> keys)
        {
            Logger.Log(LogLevels.Trace, "UP: " + string.Join(", ", keys.Select(kp => kp.ToString()).ToList()));
            foreach (SwooshAction action in SwooshConfig.CurrentConfig.SwooshActionList)
            {
                if (keys.Count() == action.Trigger.Count() && !action.Trigger.Except(keys).Any())
                {
                    Logger.Log(LogLevels.Debug, "Triggered Action: " + string.Join(", ", action.Trigger.Select(kp => kp.ToString()).ToList()) + " with: " + string.Join(", ", keys.Select(kp => kp.ToString()).ToList()));
                    action.Swoosh();
                }
            }
        }

    }

    

}
